<?php
    include_once ("MySql.php");
    include_once ("../entity/Almacen.php");
    
    class AlmacenDA extends dal{
        public function AlmacenDA(){
            parent::dal();
        }
        public function validarEliminar($codigo){
            $sql = "CALL SGLSS_VALIDAR_ELIMINAR_ALMACEN('$codigo')";
            $result = parent::ejecutar($sql);
            while ($fila = mysqli_fetch_object($result)){
                $num_row = $fila->num_row;
            }

            return $num_row;
        }
        public function eliminar($entity){
            $sql = "CALL SGLSD_ALMACEN('$entity->codigo')";
            $result = parent::ejecutar($sql);
            if($result){
                return true;
            }else{
                return false;
            }
        }
        public function editar($entity){
            $nombre = strtoupper($entity->nombre);
            $direccion = strtoupper($entity->direccion);
            $responsable = strtoupper($entity->responsable);
            
            $sql = "CALL SGLSU_ALMACEN('$entity->codigo',"
                    . "'$nombre',"
                    . "'$direccion',"
                    . "'$entity->telefono',"
                    . "'$responsable',"
                    . "'$entity->usuario_modi')";
            
            $result = parent::ejecutar($sql);
            
            if($result){
                return true;
            }else{
                return false;
            } 
        }
        public function obtenerID($entity){
            $sql = "CALL SGLSS_ALMACEN_X_COD('$entity->codigo')";
            $result = parent::ejecutar($sql);
            
            while ($fila = mysqli_fetch_object($result)){
                $obj = new Almacen();
                $obj->codigo = $fila->codigo;
                $obj->nombre = $fila->nombre;
                $obj->direccion = $fila->direccion;
                $obj->telefono = $fila->telefono;
                $obj->responsable = $fila->responsable;
            }
            return $obj;
        }
        public function listarAlmacen($codigo){
            $sql = "CALL SGLSS_ALMACEN_X_COD('$codigo')";
            $result = parent::ejecutar($sql);
            
            while ($fila = mysqli_fetch_object($result)){
                $obj = new Almacen();
                $obj->codigo = $fila->codigo;
                $obj->nombre = $fila->nombre;
                $obj->direccion = $fila->direccion;
                $obj->telefono = $fila->telefono;
                $obj->responsable = $fila->responsable;
                $almacen[]=$obj;
            }
            return $almacen;
        }
        public function insertar($entity){
            $nombre = strtoupper($entity->nombre);
            $direccion = strtoupper($entity->direccion);
            $responsable = strtoupper($entity->responsable);
            
            $sql = "CALL SGLSI_ALMACEN('$nombre',"
                    ."'$direccion',"
                    ."'$entity->telefono',"
                    ."'$responsable',"
                    ."'$entity->usuario_crea')";

            $result = parent::ejecutar($sql);

            if($result){
                return true;
            }else{
                return false;
            }
        }
        public function generarCodigo(){
            $sql = "CALL SGLSS_ALMACEN_CODIGO";
            $result = parent::ejecutar($sql);
            
            while ($fila = mysqli_fetch_object($result)){
                $obj = new Almacen();
                $obj->codigoNuevo = $fila->codigoNuevo;
            }
            return $obj;
        }
        public function numeroRegistro($nombre,$direccion){
            $sql = "CALL SGLSS_CUENTA_ALMACEN('$nombre','$direccion')";
            $result = parent::ejecutar($sql);
            $num_row=0;
            while($fila = mysqli_fetch_object($result)){
                $num_row= $fila->num_row;
            }
            return $num_row;
        }
        public function BuscarSearch($entity, $start , $limit,$sidx,$sord){
            $sql = "CALL SGLSS_ALMACEN_SEARCH('$entity->nombre',"
                    . "'$entity->direccion',"
                    . "'$start' ,"
                    . "'$limit',"
                    . "'$sidx',"
                    . "'$sord')";
            $result = parent::ejecutar($sql);
            while($fila = mysqli_fetch_object($result)){
                $obj = new Almacen();
                $obj->codigo = $fila->codigo;
                $obj->nombre = $fila->nombre;
                $obj->direccion = $fila->direccion;
                $obj->telefono = $fila->telefono;
                $obj->responsable = $fila->responsable;
                $almacen[] = $obj;
            }
            return $almacen;
        }
    }
?>
